package com.wbpay.sdk.dlg;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import com.wbpay.sdk.pay.IPay;
import com.wbpay.sdk.utils.Utils;

import java.util.List;


/**
 * 支付列表
 */

public class PayListShow {

    private Context mContext;
    private AlertDialog mPayListDialog;
    private OnPayListListener mOnPayListListener;

    public PayListShow(Context context) {
        mContext = context;
    }

    public void showPayList(final List<IPay> payList) {
        if (mPayListDialog == null || !mPayListDialog.isShowing()) {
            final String[] payItems = new String[payList.size()];
            for (int i = 0; i < payList.size(); i++) {
                payItems[i] = payList.get(i).toString();
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            int strId = Utils.getStringId(mContext, "jy_select_paymethod");
            builder.setTitle(strId);
            builder.setItems(payItems, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mPayListDialog = null;
                    IPay iPay = payList.get(which);
                    if (mOnPayListListener != null) {
                        mOnPayListListener.onPaySelected(iPay);
                    }
                }
            });
            builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    mPayListDialog = null;
                    if (mOnPayListListener != null) {
                        mOnPayListListener.onCancel();
                    }
                }
            });
            mPayListDialog = builder.create();
            mPayListDialog.show();
        }
    }

    public void setOnPayShowListener(OnPayListListener l) {
        mOnPayListListener = l;
    }

    public interface OnPayListListener {
        public void onPaySelected(IPay ipay);

        public void onCancel();
    }
}
